package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.RenewClass;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.views.RenewClassRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 续班表 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-08-23
 */
public interface RenewClassMapper extends BaseMapper<RenewClass> {

    /**
     * 获取续班率老师，按校区
      * @param wrapper
     * @return
     */
    @Select("select * from renew_record_view ${ew.customSqlSegment}")
    public abstract List<RenewClassRecord> getRenewRecord(@Param(Constants.WRAPPER) Wrapper wrapper);


    /**
     * 获取续班率老师，按个人
     * @param wrapper
     * @return
     */
    @Select("select * from renew_record_person_view ${ew.customSqlSegment}")
    public abstract List<RenewClassRecord> getRenewRecordPerson(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 获取续班率老师，按校区
     * @param wrapper
     * @return
     */
    @Select("select * from recordclass_school_view ${ew.customSqlSegment}")
    public abstract List<RenewClassRecord> getRenewRecordSchool(@Param(Constants.WRAPPER) Wrapper wrapper);



    /**
     * 老师带班总人数
     * @return
     */
    //@Select("select count(id) as count from student where id in (select student_id from student_grade where school_id = #{schoolId} and subjects_id = #{subjectsId} and year_part = #{yearPart} and quarter_num = #{quarterNum} and status<>3 and reading_status<>3 and grade_id in(select id from grade where teacher_id = #{renewTeacherId})) ")
    @Select("select count(DISTINCT student_id) as count from student_grade ${ew.customSqlSegment}")
    public abstract Integer getStudentAllCount(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 应续班人数
     * @param schoolId
     * @param subjectsId
     * @param renewTeacherId
     * @param yearPart
     * @param quarterNum
     * @return
     */
    @Select("select count(a.id) as count from student_grade  as a LEFT JOIN course as b on a.course_id = b.id where a.school_id = #{schoolId} and a.subjects_id = #{subjectsId}  and a.year_part = #{yearPart} and a.quarter_num = #{quarterNum} and a.renew_teacher_id =#{renewTeacherId} and b.year_class_id <> 9")
    public abstract Integer getShouldRenewCount(@Param("schoolId") Long schoolId,@Param("subjectsId") Long subjectsId,@Param("renewTeacherId") Long renewTeacherId,@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum);

    /**
     * 已续班人数
     * @return
     */
    @Select("select count(id) as count from renew_class where school_id = #{schoolId} and subjects_id = #{subjectsId} and teacher_id = #{renewTeacherId} and year_part = #{yearPart} and quarter_num = #{quarterNum} and status=1")
    public abstract Integer getAlreadyRenewCount(@Param("schoolId") Long schoolId,@Param("subjectsId") Long subjectsId,@Param("renewTeacherId") Long renewTeacherId,@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum);

    /**
     * 流失人数
     * @return
     */
    @Select("select count(id) as count from renew_runaway where school_id = #{schoolId} and subjects_id = #{subjectsId} and teacher_id = #{renewTeacherId} and year_part = #{yearPart} and quarter_num = #{quarterNum}")
    public abstract Integer getRunOffCount(@Param("schoolId") Long schoolId,@Param("subjectsId") Long subjectsId,@Param("renewTeacherId") Long renewTeacherId,@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum);
}
